package hn.cch.handler;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.FilterEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TextLineHandler implements IoHandler {

    private static final Logger logger = LoggerFactory.getLogger(IoHandlerAdapter.class);

    @Override
    public void sessionCreated(IoSession ioSession) throws Exception {
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
    }

    @Override
    public void sessionIdle(IoSession ioSession, IdleStatus idleStatus) throws Exception {

    }

    @Override
    public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception {
        if (logger.isWarnEnabled()) {
            logger.warn("EXCEPTION, please implement " + this.getClass().getName() + ".exceptionCaught() for proper handling:", throwable);
        }
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        // super.messageReceived(session, message);

        logger.info("Received:{}", message.toString());

    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        // super.messageSent(session, message);

        logger.info("Sent:{}", message.toString());
    }

    @Override
    public void inputClosed(IoSession ioSession) throws Exception {
        ioSession.closeNow();
    }

    @Override
    public void event(IoSession ioSession, FilterEvent filterEvent) throws Exception {

    }
}
